package com.mc.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mc.book.dao.BookDao;
import com.mc.book.pojo.PageBean;
import com.mc.book.pojo.Result;
import com.mc.book.pojo.request.*;
import com.mc.book.redis.RedisService;
import com.mc.book.service.BookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class BookServiceImpl implements BookService {
	private static final Logger log = LoggerFactory.getLogger(BookServiceImpl.class);

    @Autowired
    RedisService redis;

    @Autowired
    BookDao bookDao;

    @Override
    public List<RequestBook> selectBookList(RequestBook book)
    {
        return bookDao.selectBookList(book);
    }


    @Override
    public void userUploadBook(RequestUserUploadBook request) {
        bookDao.userUploadBook(request);
    }


    @Override
    public Result getBookByName( RequestBook request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RequestBook> list = bookDao.getBookByNamePage(request.getBook_name());
        return Result.success(new PageInfo<>(list));
    }

    @Override
    public Result<RequestBook> getBookById( RequestBook request) {
        RequestBook book = bookDao.getBookById(request.getId());
        return Result.success(book);
    }

    @Override
    public Result queryBookIndex(RequestBookIndex request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<RequestBook> list = bookDao.queryBookIndexPage(request.getBook_id());
        return Result.success(new PageInfo<>(list));
    }

    @Override
    public Result queryBookContent(RequestBookContent request) {
        RequestBookContent content = bookDao.queryBookContent(request.getIndex_id());
        return Result.success(content);
    }

    @Override
    public Result bookCategory() {
        List<RequestBookCategory> list = bookDao.getBookCategory();
        return Result.success(list);
    }

    @Override
    public Result getBookByCategory(RequestBookCategory request) {
        if(null != request.getPageNum() && null != request.getPageSize()){
            PageHelper.startPage(request.getPageNum(), request.getPageSize());
            List<RequestBook> list = bookDao.getBookByCategoryPage(request.getId());
            return Result.success(new PageInfo<>(list));
        }else {
            List<RequestBook> list = bookDao.getBookByCategoryPage(request.getId());
            request.setPageNum(0);
            request.setPageSize(0);
            return Result.success(new PageInfo<>(list));
        }

    }


}
